package com.dengc.controller;

import com.dengc.pojo.SysUser;
import com.dengc.service.SysUserService;
import com.dengc.utils.Contants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    SysUserService sysUserService;

    @RequestMapping("/tologin")
    public String tologin(){
        return "login";
    }


    //登录界面
    @RequestMapping("/login")
    public String login(@RequestParam("account") String account,
                        @RequestParam("password") String password,
                        HttpServletRequest request, HttpSession session
    ){
        SysUser sysUser = sysUserService.login(account,password);
        if (sysUser!=null){
            session.setAttribute(Contants.USERSESSION,sysUser);
            return "main";
        }else {
            request.setAttribute("error","用户名或密码错误！");
            return "login";
        }
    }

    //登出页面
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute(Contants.USERSESSION);
        return "login";
    }
}
